package demo.helloworld.service;

import act.db.ebean.EbeanDao;
import demo.helloworld.dto.Statistics;
import demo.helloworld.dto.Types;
import demo.helloworld.model.Attach;
import demo.helloworld.model.Comments;
import demo.helloworld.model.Contents;
import demo.helloworld.model.Metas;

import javax.inject.Inject;
import java.util.List;

/**
 * Created by vilen on 2017/3/1.
 */
public class SiteService {
    @Inject
    private EbeanDao<Integer, Comments> dao;
    public List<Comments> listAllComments() {
        return dao.findAllAsList();
    }

    public List<Comments> recentComments(int limit) {
        // setMaxRow 和 subList() sql 不一样
        return dao.ebean().find(Comments.class).setMaxRows(limit).findList();
    }

    public List<Contents> recentContents(int limit) {
        return dao.ebean().find(Contents.class).setMaxRows(limit).findList();
    }

    public Statistics getStatistics() {
        // dto
        Statistics statistics = new Statistics();
        // todo 这里可以改进 dao.count 但是注意 EbeanDao<Integer, Comments> dao;
        statistics.setArticles(dao.ebean().find(Contents.class).where().eq("type", Types.ARTICLE).eq("status", Types.PUBLISH).findCount());
        statistics.setAttachs(dao.ebean().find(Attach.class).findCount());
        statistics.setComments((int) dao.count());
        statistics.setLinks(dao.ebean().find(Metas.class).where().eq("type", Types.LINK).findCount());
        return statistics;
    }
}
